package com.jxnu.escms.Control.HealthPunch;

import com.jxnu.escms.Control.UserControl.UserControl;
import com.jxnu.escms.Service.HealthService.impl.HealthService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yihui
 * 健康打卡组件
 */
@Slf4j
@RestController
public class HealthPunch {

    @Resource
    private HealthService healthService;

    @Resource
    private UserControl userControl;

    @RequestMapping("/health")
    public Map healthPunch(@RequestBody Map<String, Object> punchData) throws ParseException {
        // 设置时间格式
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        // 将核检时间修改为上述格式
        Map<String, String> data = (Map<String, String>) punchData.get("data");
        data.replace("date", df.format(df.parse(data.get("date"))));

        // 获取用户id
        int userId = (int) punchData.get("user_id");
        // 增加打卡记录
        int i = healthService.addPunch(userId, data);

        // 更新用户的健康码信息
        healthService.updateUserHealth(data.get("color"), userId);

        Map<String, String> res = new HashMap<>();
        res.put("punch", i == 1? "success":"worry");
        return res;
    }

    @RequestMapping("/addr/{id}")
    public List getAddr(@PathVariable("id") int id){
        return userControl.parseAddress(userControl.getAddress(null, id));
    }
}
